package com.javalong.affairs.base.ui;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.javalong.affairs.base.constant.Constant;
import com.javalong.affairs.base.constant.MapKey;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.data.AllData;
import com.javalong.affairs.base.inter.IPager;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.lidroid.xutils.http.RequestParams;

public class SimplePullRefreshList extends PullToRefreshListView
        implements
        IPager,
        com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener<ListView> {
    private final static int SUCCESS = 1;
    private int mPage;
    private String actionName;
    private String methodName;
    private RequestParams params;
    private int dataResId;
    private BaseAdapter adapter;
    private Type dataType;
    private Map<String, Object> paramMap;
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case SUCCESS:
                    if (adapter != null) {
                        adapter.notifyDataSetChanged();
                        SimplePullRefreshList.this.onRefreshComplete();
                    }
                    break;
                default:
                    break;
            }
        }

        ;
    };

    public SimplePullRefreshList(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SimplePullRefreshList(
            Context context,
            com.handmark.pulltorefresh.library.PullToRefreshBase.Mode mode,
            com.handmark.pulltorefresh.library.PullToRefreshBase.AnimationStyle style) {
        super(context, mode, style);
        init();
    }

    public SimplePullRefreshList(Context context,
                                 com.handmark.pulltorefresh.library.PullToRefreshBase.Mode mode) {
        super(context, mode);
        init();
    }

    public SimplePullRefreshList(Context context) {
        super(context);
        init();
    }

    private void init() {
        this.setMode(Mode.PULL_FROM_START);
        this.setOnRefreshListener(this);
    }

    public void initData(Map<String, Object> data) {
        actionName = data.get(MapKey.ACTION_NAME).toString();
        methodName = data.get(MapKey.METHOD_NAME).toString();
        dataResId = (Integer) data.get(MapKey.DATA_RESID);
        dataType = (Type) data.get(MapKey.DATA_TYPE);
        paramMap = (Map<String, Object>) data.get(MapKey.REQUEST_PARAMS);
    }

    @Override
    public void refresh() {
        mPage = 0;
        AllData.GetInstance().clearList(dataResId);
        requestData();
    }

    @Override
    public void loadMore() {
        mPage++;
        requestData();
    }

    private void requestData() {
        String url = UrlUtils.getUrl(actionName, methodName);
        if (paramMap == null) {
            paramMap = new HashMap<String, Object>();
        }
//        paramMap.put(RequestKey.PAGEINDEX, mPage + "");
//        paramMap.put(RequestKey.PAGESIZE, Constant.PAGE_SIZE + "");
        HttpUtils.post(url, paramMap, new BaseResponseListener(dataType) {
            @Override
            public void onSuccess(Object obj) {
                super.onSuccess(obj);
                List<?> tempList = (List<?>) obj;
                AllData.GetInstance().appendListData(dataResId, tempList);
                Message msg = Message.obtain();
                msg.what = SUCCESS;
                handler.dispatchMessage(msg);
                if (onLoadSuccess != null) {
                    onLoadSuccess.onSuccess();
                }
            }
        });
    }

    @Override
    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        if (isHeaderShown()) {
            refresh();
        } else if (isFooterShown()) {
            loadMore();
        }
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        super.setAdapter(adapter);
        if (adapter instanceof BaseAdapter) {
            this.adapter = (BaseAdapter) adapter;
        }
    }

    public interface OnLoadSuccess {
        void onSuccess();
    }

    private OnLoadSuccess onLoadSuccess;

    public void setOnLoadSuccess(OnLoadSuccess onLoadSuccess) {
        this.onLoadSuccess = onLoadSuccess;
    }

}
